﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class GroupBO : BaseBO
    {
        public const string FIELD_GROUP_ID = "GroupId";
        public const string FIELD_NAME = "GroupName";
        public const string FIELD_CODE = "GroupCode";
        public const string FIELD_DESCRIPTIONS = "Description";
        public const string FIELD_APPID = "AppId";
        public const string FIELD_TYPE = "Type";
        public const string FIELD_DELETED = "IsActive";
        public const string FIELD_RECORD_COUNT = "RecordCount";
        
        public string GroupCode{get; set;}

        private String _connectionString;

        public GroupBO(string connString)
        {
            this._connectionString = connString;

        }
        public GroupBO()
        {
            this._connectionString = Common.ConnectionUtil.DHQG_ADMIN_CONNECTION_STRING;

        }

        public GroupDAO _groupDAO;

        public GroupDAO CreateSQLDAO()
        {
            return new GroupDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<GroupModel> GetDynamicListPaging(GroupModel aModel, int pageSize, int pageIndex,bool compareEqual)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE},
                    {GroupModel.FIELD_APPID,FIELD_APPID},
                    {GroupModel.FIELD_TYPE,FIELD_TYPE},
                    {GroupModel.FIELD_DESCRIPTIONS,FIELD_DESCRIPTIONS},
                    {GroupModel.FIELD_DELETED,FIELD_DELETED},                    
                    {GroupModel.FIELD_RECORD_COUNT,FIELD_RECORD_COUNT}                  
                };
                return _groupDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary,compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public GroupModel GetGroupById(int? appId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE}, 
                    {GroupModel.FIELD_APPID,FIELD_APPID}, 
                    {GroupModel.FIELD_TYPE,FIELD_TYPE}, 
                    {GroupModel.FIELD_DESCRIPTIONS,FIELD_DESCRIPTIONS},
                    {GroupModel.FIELD_DELETED,FIELD_DELETED}
                    
                };

                return _groupDAO.GetById(appId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool createOrUpdateGroup(GroupModel appModel, int[] nhomCoBan)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                return _groupDAO.createOrUpdateGroup(appModel, nhomCoBan);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool deleteGroup(int[] appId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                return _groupDAO.deleteGroup(appId);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<GroupModel> GetAllAppCode(string AppCode)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE},
                    {GroupModel.FIELD_TYPE,FIELD_TYPE}
                };
                return _groupDAO.GetAllAppCode(AppCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<CheckBoxList> GetAllByUserName(string UserName, string AppCode)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {CheckBoxList.FIELD_ID,"Id"},
                    {CheckBoxList.FIELD_NAME,"Name"}
                };
                return _groupDAO.GetAllByUserName(UserName,AppCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<GroupModel> GetAll(int? AppId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE}
                };
                return _groupDAO.GetAll(AppId,mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public IEnumerable<GroupModel> GetAllGroupByUserIdAndAppCode(int userId, string appCode)
        {
            try
            {
                _groupDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE},  
                    {GroupModel.FIELD_DESCRIPTIONS,FIELD_DESCRIPTIONS},
                    {GroupModel.FIELD_DELETED,FIELD_DELETED}
                    
                };

                return _groupDAO.GetAllByUserIdAndAppCode(userId, appCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool CheckGroupExist(string _groupcode)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                return _groupDAO.CheckGroupExist(_groupcode);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool CheckGroupExist(string groupCode, string appCode)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                return _groupDAO.CheckGroupExist(groupCode, appCode);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string GetGroupNameByGroupId(int? groupId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    //{GroupModel.FIELD_CODE,FIELD_CODE}
                };
                return _groupDAO.GetGroupNameByGroupId(groupId,mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string GetGroupCodeByGroupId(int? groupId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_CODE,FIELD_CODE},
                    //{GroupModel.FIELD_CODE,FIELD_CODE}
                };
                return _groupDAO.GetGroupCodeByGroupId(groupId, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataPage<GroupModel> GetDynamicListPagingd(GroupModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE},  
                    {GroupModel.FIELD_DESCRIPTIONS,FIELD_DESCRIPTIONS},
                    {GroupModel.FIELD_DELETED,FIELD_DELETED},                    
                    {GroupModel.FIELD_RECORD_COUNT,FIELD_RECORD_COUNT}                  
                };
                return _groupDAO.GetDynamicListPagingd(aModel, pageSize, pageIndex, mapDictionary, compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool reActive(int appId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                return _groupDAO.reActive(appId);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// quyvd, 20150827, lay theo groupcode
        /// </summary>
        /// <param name="enumCode"></param>
        /// <returns></returns>
        public List<ActionGroupCode> GetAllUserGroupCode(string GROUPCODE)
        {
            try
            {
                _groupDAO = CreateSQLDAO();

                List<ActionGroupCode> result = _groupDAO.GetAllUserGroupCode(GROUPCODE);

                return result;
            }
            catch
            {
                throw;
            }
        }

       public List<GroupModel> GetListGroupByApp(int AppId)
        {
            try
            {
                _groupDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE}
                };
                return _groupDAO.GetListGroupByApp(AppId,mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
       public List<GroupModel> GetListGroupByAppIdAndNhomCoBanId(int appId, int nhomCoBanId)
       {
           try
           {
               _groupDAO = CreateSQLDAO();
               Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {GroupModel.FIELD_GROUP_ID,FIELD_GROUP_ID},
                    {GroupModel.FIELD_NAME,FIELD_NAME},
                    {GroupModel.FIELD_CODE,FIELD_CODE}
                };
               return _groupDAO.GetListGroupByAppIdAndNhomCoBanId(appId, nhomCoBanId, mapDictionary);
           }
           catch
           {

               throw;
           }
       }
    }
}
